;(function ( $, window, document, undefined ) {

    var pluginName = 'hexagonplugin',
        defaults = {
            propertyName: "value"
        };

    // The actual plugin constructor
    function Plugin( element, options ) {
        this.element = element;

        this.options = $.extend( {}, defaults, options) ;
        
        this._defaults = defaults;
        this._name = pluginName;
        
        this.init();
    }

    Plugin.prototype.init = function () {
		
		var container = this.element;
		var slides = $(this.element).children().children();
		var slidesCount = slides.length;
		var maxElements = 3;
		var count = countElements();
		var offset = 0;
		var r = 0;
		var left = 100/count;
		var lastElements = [];
		var page = 0;
		var pages = Math.ceil(slidesCount/count);
		var windowWidth = $(window).width();

		init();
		changeSlides(0);

		$(window).resize($.throttle(300, function(){
			if(windowWidth !== $(window).width()) {
			resizeElements();
			count = countElements();
			left = 100/count;
			offset = 0;
			page = 0;
			pages = Math.ceil(slidesCount/count);
			}
		})); 
		function init() {
			$(container).find('ul').height($(container).find('li').height());
			for(var i = 0; i<= slidesCount; i++) {
				$(slides[i]).css('left', r*left+'%');
				r = r+1;
				if(r === count)
					r = 0;
			}
		}

		function resizeElements() {
			r = 0;
			for(var i = 0; i<= slidesCount; i++) {
				$(slides[i]).css('left', r*left+'%');
				r = r+1;
				if(r >= count)
					r = 0;
			}
			$(container).find('ul').height($(container).find('li').height());
			changeSlides(0);
		}

		function countElements() {
			var contentWidth = $('.slider-sizer').width();
			return Math.floor(($(container).width()) / (contentWidth-1));
		}

		function changeSlides(page) {
			var nextPage = (page+1 === pages ? 0 : page+1);
			var prevPage = (page-1 < 0 ? pages-1 : page-1);
			
			lastElements = [];
			offset = page*count;
			
			for(var i = (nextPage*count); i < nextPage*count+count; i++) {
				$(slides[i]).addClass('animate-next').removeClass('animate-prev').removeClass('animate').removeClass('visible');
			}
			
			for(var i = (prevPage*count); i < prevPage*count+count; i++) {
				$(slides[i]).addClass('animate-prev').removeClass('animate-next').removeClass('animate').removeClass('visible');
			}	
			
			for (var i = offset; i < count+offset; i++) {
			
				if(slides[i]){
					$(slides[i]).addClass('animate');
					lastElements.push(slides[i]);
				}
			}
			$(container).find('.animate').each(function(index, value){
				$(this).addClass('visible').removeClass('animate-prev').removeClass('animate-next');
			});
			
			offset = offset+count;
			if(offset >= slidesCount){
				offset = 0;
			}
			
		}

		function forward() {
			page++;
			if(page >= pages)
				page = 0;
			changeSlides(page);
		}

		function backward() {
			page--;
			if(page < 0)
				page = pages-1;
			changeSlides(page);
		}

		$(container).bind('forward', function() {
			forward();
		});
		$(container).bind('backward', function() {
			backward();
		});

	};

    // A really lightweight plugin wrapper around the constructor, 
    // preventing against multiple instantiations
    $.fn[pluginName] = function ( options ) {
        return this.each(function () {
            if (!$.data(this, 'plugin_' + pluginName)) {
                $.data(this, 'plugin_' + pluginName, 
                new Plugin( this, options ));
            }
        });
    }

})( jQuery, window, document );